<template>
  <div class="app-container">
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="申请会员" prop="user.user" align="center" />
      <el-table-column label="实名认证姓名" prop="user.name" align="center" />
      <el-table-column label="小店名称" prop="name" align="center" />
      <el-table-column label="所在地" align="center">
        <template
          slot-scope="{ row }"
        >{{
          row.province && row.province.length > 0
            ? row.province[0].title
            : ""
        }}{{ row.city && row.city.length > 0 ? row.city[0].title : ""
        }}{{
          row.country && row.country.length > 0 ? row.country[0].title : ""
        }}{{
          row.area && row.area.legth > 0 ? row.area[0].title : ""
        }}</template>
      </el-table-column>
      <el-table-column label="申请时间" prop="created_at" align="center" />
      <el-table-column label="状态" align="center">
        <template>待审核</template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            plain
            type="primary"
            @click="handleSubmit(row)"
          >
            同意
          </el-button>
          <el-button
            size="mini"
            plain
            type="danger"
            @click="handleRefuse(row.id)"
          >
            拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getShopping, updateShopping } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'DistributionsExamine',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      type: 0,
      listQuery: {
        page: 1,
        per_page: 20,
        type: 0,
        audit_status: 0
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      getShopping(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleSubmit(row) {
      const that = this
      this.$confirm('请确认是否同意申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          updateShopping(row.id, {
            type: '0',
            audit_status: '1'
          })
            .then(() => {
              that.getList()
              that.$notify({
                title: '提示',
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
              that.getList()
            })
            .catch(() => {
              this.listLoading = false
            })
        })
        .catch(() => {})
    },
    // 拒绝
    handleRefuse(id) {
      const that = this
      this.$prompt('', '拒绝', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputPlaceholder: '请输入拒绝原因',
        inputErrorMessage: '拒绝原因不能为空',
        inputType: 'textarea',
        closeOnPressEscape: false,
        closeOnClickModal: false,
        closeOnHashChange: false,
        callback() {},
        beforeClose(action, instance, done) {
          if (action === 'confirm') {
            if (instance.confirmButtonLoading) {
              return false
            }
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '操作中...'
            updateShopping(id, {
              type: '0',
              reject_remark: instance.inputValue,
              audit_status: '2'
            })
              .then(() => {
                that.getList()
                that.$notify({
                  title: '提示',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                })
                done()
              })
              .finally(() => {
                instance.confirmButtonLoading = false
                instance.confirmButtonText = '确定'
              })
          } else {
            if (!instance.confirmButtonLoading) {
              done()
            } else {
              that.$message.warning('操作中，禁止关闭')
            }
          }
        }
      })
    }
  }
}
</script>
